package com.matace.keychain.domain;

public class LoginUseCase {
    private final UserRepository userRepository;

    public LoginUseCase(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public boolean login(String username, String password) {
        // 判断 username 是邮箱还是名字
        boolean isEmail = username != null && username.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
        if (isEmail) {
            // 邮箱登录
            return userRepository.loginByEmail(username, password);
        } else {
            // 用户名登录
            return userRepository.loginByName(username, password);
        }
    }

    public boolean isRememberMe() {
        return userRepository.isRememberMe();
    }

    public String getSavedUsername() {
        return userRepository.getSavedUsername();
    }

    public String getSavedPassword() {
        return userRepository.getSavedPassword();
    }

    public void saveCredentials(String username, String password) {
        userRepository.saveCredentials(username, password);
    }

    public void clearCredentials() {
        userRepository.clearCredentials();
    }

}